<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/4/16
 * Time: 11:26
 */

namespace app\model;


class Product extends Base
{
    protected $append = [
        'price',
        'price_list'
    ];
    protected $autoWriteTimestamp = 'datetime';

    public function user(){
        return $this->hasOne(User::class,'id','user_id')->field('id,username,corporate_name,realname,mobile');
    }

    public function getPriceAttr($value,$data){
        $model = new Quotation();
        $select = $model->is_new(true)
            ->field('price,weight')
            ->where('name',$data['name'])
            ->where('model',$data['model'])
            ->where('attr',$data['attr'])
            ->where('weight','>',$data['weight'])
            ->where('stock','<>',0)
            ->select();
        if (empty($select->toArray())) return 0;
        $cost = cache($data['user_id'].'cost');
        if (!empty($cost) && !empty($cost['artificial'])){
            $artificial = bcdiv($cost['artificial'],1000,6);
        }else{
            $artificial = 0;
        }
        foreach ($select AS $key => $value){
            $min_price = empty($min_price) ? $value->price : $min_price;
            $min_total_price = empty($min_total_price) ? bcmul($value->price,$value->weight,2) : $min_total_price;
            if (bcmul($value->price,$value->weight,2) < $min_total_price){
                $min_price = $value->price;
                $min_total_price = bcmul($value->price,$value->weight,2);
            }
        }
        $min_price = bcadd($data['blank_processing_fee'],$min_price + $artificial,2);
        return $min_price;
    }

    public function getPriceListAttr($value,$data){
        $model = new Material();
        $max_price = $model->is_new(true)
            ->where('name',$data['material'])
            ->where('attr',$data['material_attr'])
            ->max('price');
        $min_price = $model->is_new(true)
            ->where('name',$data['material'])
            ->where('attr',$data['material_attr'])
            ->min('price');
        $model = new Quotation();
        $select = $model->is_new(true)
            ->field('supplier,stock,price,weight,create_time')
            ->where('name',$data['name'])
            ->where('model',$data['model'])
            ->where('attr',$data['attr'])
            ->where('weight','>',$data['weight'])
            ->where('stock','<>',0)
            ->where('stock','<>','')
            ->select();
        $array = [];
        foreach ($select AS $key => $value) {
            $item = $value->toArray();
            $item['waste'] = bcsub($value['weight'], $data['weight'], 8);
            $item['min_waste_fee'] = bcmul($item['waste'], $min_price / 1000, 2);
            $item['max_waste_fee'] = bcmul($item['waste'], $max_price / 1000, 2);
            $item['predict_final_price'] = bcadd($item['price'], $data['blank_processing_fee'], 2);
            $item['waste'] = bcadd($item['waste'], 0, 3);
            $item['pate'] = bcdiv($item['min_waste_fee'] * 100, $item['predict_final_price'], 2);
            $array[$key] = $item;
        }
        // 使用 usort 进行排序
        usort($array, 'compareByPateDesc');
        return $array;
    }
}